package com.backend.controller;


import com.backend.pojo.domain.User;
import com.backend.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/users")
@Slf4j
//不加的话虽然数据能正常传输，但是前端页面会没有正常响应
public class UserController {
    @Autowired
    private UserService userService;

    // 1 查询所有(get请求)
    @GetMapping
    public List<User> User_selectAll() {
        log.info("查询所有用户信息");
        return userService.selectAll();
    };

    // 2 根据userId查询
    @GetMapping("/{userId}")
    public User User_selectById(@PathVariable String userId) {
        return userService.selectById(userId);
    };

    // 3 多条件模糊查询(userName,userId,sort)
    @PostMapping("/selectByConditionMulti")
    public List<User> User_selectByConditionMulti(@RequestBody User user) {
        log.info("根据用户条件模糊查询:{}",user);
        return userService.selectByConditionMulti(user);
    };

    // 4 多条件中选择一个条件模糊查询
    @PostMapping("/selectByConditionSingle")
    public List<User> User_selectByConditionSingle(@RequestBody User user) {
        return userService.selectByConditionSingle(user);
    };

    // 5 增加
    @PostMapping("/addUser")
    public boolean User_addUser(@RequestBody User user) {
        log.info("用户注册:{}",user);
        return userService.addUser(user);
    };

    // 6 根据userId修改
    @PutMapping
    public boolean User_update(@RequestBody User user) {
        log.info("修改用户信息");
        return userService.update(user);
    };

    // 7 根据userId删除一个
    @DeleteMapping("/{userId}")
    public boolean User_deleteUserById(@PathVariable String userId) {
        return userService.deleteUserById(userId);
    };

//    @DeleteMapping("/deleteManyUserByIds")
//    public boolean User_deleteManyUserByIds(@RequestParam("userIds") int[] userIds) {
//        return userService.deleteManyUserByIds(userIds);
//    };
    @DeleteMapping("/deleteManyUserByIds")
    public boolean User_deleteManyUserByIds(@RequestBody String[] userIds) {
        return userService.deleteManyUserByIds(userIds);
    };
}
